<?php

namespace app\common\model\Product;

use think\Model;
use traits\model\SoftDelete;

class Product extends Model
{
    // 表名
    protected $name = 'product';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 软删除
    use SoftDelete;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'flag_text',
        'status_text',
        'cover_cdn',
        'thumbs_cdn',
        'rent_status_text',
        'thumbs_text'
    ];

    public function getFlagList()
    {
        return ['1' => __('新品'), '2' => __('热销'), '3' => __('推荐')];
    }


    public function getFlagTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
        $list = $this->getFlagList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusList()
    {
        return ['0' => __('下架'), '1' => __('上架')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getRentStatusList()
    {
        return ['1' => '普通商品', '2' => '仅租商品', '3' => '既购买也能租赁的商品'];
    }

    public function getRentStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['rentstatus']) ? $data['rentstatus'] : '');
        $list = $this->getRentStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    // 时间修改器
    public function setCreatetimeAttr($value, $data)
    {
        return strtotime($value);
    }

    // 分类关联查询
    public function type()
    {
        return $this->belongsTo('app\common\model\Product\Type', 'typeid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    // 单位关联查询
    public function unit()
    {
        return $this->belongsTo('app\common\model\Product\Unit', 'unitid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getCoverCdnAttr($value, $data)
    {
        $cdn = config('site.url');

        $thumbs = empty($data['thumbs']) ? [] : trim($data['thumbs']);

        if (empty($thumbs)) {
            return $cdn . '/assets/home/images/cbd.jpg';
        }

        //字符串替换
        $thumbs = str_replace("/uploads/", $cdn . "/uploads/", $thumbs);

        //在转换为数组
        $list = explode(',', $thumbs);

        if (count($list) > 0) {
            return $list[0];
        }
    }

    public function getThumbsCdnAttr($value, $data)
    {
        //获取到cdn的地址
        $cdnurl = config('site.cdnurl') ? config('site.cdnurl') : '';
        $cdnurl = trim($cdnurl, '/');

        // 多张图字符串结构
        $thumbs = isset($data['thumbs']) ? $data['thumbs'] : '';

        if (empty($thumbs)) {
            $thumbs = "/assets/home/images/cbd.jpg";
        } else {
            $thumbs = explode(',', $thumbs);
            if (!empty($thumbs)) {
                $pic = '';

                foreach ($thumbs as $item) {
                    if (is_file("." . $item)) {
                        $pic = $item;
                        break;
                    }
                }

                if (empty($pic)) {
                    $pic = "/assets/home/images/cbd.jpg";
                }

                $thumbs = $pic;
            }
        }

        return $cdnurl . $thumbs;
    }

    public function getThumbsTextAttr($value, $data)
    {
        //获取到cdn的地址
        $cdnurl = config('site.cdnurl') ? config('site.cdnurl') : '';
        $cdnurl = trim($cdnurl, '/');

        // 多张图字符串结构
        $thumbs = isset($data['thumbs']) ? $data['thumbs'] : '';
        $arr = explode(',', $thumbs);

        //返回结果
        $list = [];

        foreach ($arr as $item) {
            if (is_file("." . $item)) {
                $list[] = $cdnurl . $item;
            }
        }

        if (empty($list)) {
            $list[] = $cdnurl . "/assets/img/shop.jpg";
        }

        return $list;
    }
}
